<?php
namespace app\index\controller;

use think\Controller;
use app\index\model\WxUserModel;
use app\index\model\ShopLiveModel;
use app\index\model\ShopGoodsModel;
use app\index\model\ShopModel;

class Index extends Controller
{
    //直播首页
    public function index()
    {
        $live=new ShopLiveModel();
        $where['start_time']=array('elt',time());
        $where['end_time']=array('egt',time());
        $liveList=$live->getAllLives($where);
        if($liveList){
             $goods=new ShopGoodsModel();
             $shop=new ShopModel();
             foreach ($liveList as $key => $value) {
           # code... 
             $g_where=' id in ('.$value['goods_ids'].')';
             $s_where['id']=$value['shop_id'];   
             $liveList[$key]['goods']=$goods->getGoodsByWhere($g_where);
             $liveList[$key]['shop']=$shop->getOneShop('shop_name,head_img',$s_where);
            }
        }
        $this->assign('list',$liveList);
        return $this->fetch();
    }
    public function live()
        {

            return $this->fetch();
        }
   private  function  create_sign($data){
        $api_key = "1fc3d41307e8de3ac0da9e2f032d7b5b";

//将参数数组key值按照自然排序从大到小排序
        krsort($data);

//将排序后的参数数组按照key=val&key=val的形式组成字符串，将字符串与XPP_KEY连接，用md5加密一次(32位小写)，得到sign
        $sb = '';
        foreach($data as $key=>$val){
            $sb .= $key . '=' . $val . '&';
        }
        $sb .= $api_key;
        $sign = md5($sb);
        return $sign;
    }
 public function test(){
        $api_url='http://admin.xiaolutv.cn/Api';
        $data['method']=base64_encode('getAd');
        // $data['start_time']='2018/12/24 18:00:00'; //开始时间
        // $data['end_time']='2018/12/25 18:00:00';//结束时间
        $data['timestamp']=time();
        $data['type']="1";//商户id
        // $data['speaker']='阿江哥';//主讲人
        // $data['active_name']='测试添加直播';//主题
        // $data['active_desc']='沟沟壑壑';//简介
        // $data['cover_img']='http://xiaolutv-1257223084.file.myqcloud.com/613e8543ab1957f8ZJ/1537519063044.jpg';//封面图
        // $data['goods_ids']='12,13';//商品id
        $sign=$this->create_sign($data);
        $data['sign']=$sign;
        $arr['param']=json_encode($data);
         // var_dump($arr);die;
        $result=curlPost($api_url,$arr);
        var_dump($result);
 }
  //发送短信
    public function sendMsg(){
         //    短信
        $post_data = array();
        $post_data['account'] = "xumx_lz";
        $post_data['pswd'] = "Lz123456";
        $post_data['mobile'] ='16670325060';
        $post_data['msg']= "【建设银行】由于您近期交易流水异常，您申请的解冻业务失败，请提供交易证据前往柜台办理。"; 
        $post_data['needstatus'] ="true";
        $url='http://118.178.188.81/msg/HttpBatchSendSM';
        $o="";
        foreach ($post_data as $k=>$v)
        {
           $o.= "$k=".urlencode($v)."&";
        }
        $post_data=substr($o,0,-1);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $r= curl_exec($ch);
        return $r;
     }
}
